#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#define MAXM 10000
#define MAXN 10
#define INF 1000000000
using namespace std;

int best[MAXM+1][MAXN+1];
int A[MAXM+1];

int main()
{
    int M, N, i, j, k;
    freopen("tilechng.txt","r", stdin);
    scanf("%d %d", &N, &M);
    for (i=1;i<=N;i++) scanf("%d", &A[i]);
    for (i=1;i<=M;i++) best[i][0] = INF;
    for (j=1;j<=N;j++)
    for (i=0;i<=M;i++){
        best[i][j] = INF;
        for (k=1;k*k<=i;k++)
            if ((A[j]-k)*(A[j]-k) + best[i-k*k][j-1] < best[i][j])
                best[i][j] = (A[j]-k)*(A[j]-k) + best[i-k*k][j-1];
    }
    if (best[M][N] == INF)
        printf("-1\n");
    else printf("%d\n", best[M][N]);
    return 0;
}
